﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace cstdx.Proto.Std;

/// <summary>
/// 财务信息原始响应结构（直接映射二进制数据）
/// </summary>
internal class FinanceInfoResponseRaw
{
	/// <summary>
	/// 未知用途的2字节数据
	/// </summary>
	public byte[] Unknown1 { get; set; } = new byte[2];

	/// <summary>
	/// 市场类型
	/// </summary>
	public int Market { get; set; }

	/// <summary>
	/// 股票代码
	/// </summary>
	public string Code { get; set; } = string.Empty;

	/// <summary>
	/// 流通股本
	/// </summary>
	public float LiuTongGuBen { get; set; }

	/// <summary>
	/// 省份代码
	/// </summary>
	public ushort Province { get; set; }

	/// <summary>
	/// 行业代码
	/// </summary>
	public ushort Industry { get; set; }

	/// <summary>
	/// 更新日期
	/// </summary>
	public uint UpdatedDate { get; set; }

	/// <summary>
	/// 上市日期
	/// </summary>
	public uint IPODate { get; set; }

	/// <summary>
	/// 总股本
	/// </summary>
	public float ZongGuBen { get; set; }

	/// <summary>
	/// 国家股
	/// </summary>
	public float GuoJiaGu { get; set; }

	/// <summary>
	/// 发起人法人股
	/// </summary>
	public float FaQiRenFaRenGu { get; set; }

	/// <summary>
	/// 法人股
	/// </summary>
	public float FaRenGu { get; set; }

	/// <summary>
	/// B股
	/// </summary>
	public float BGu { get; set; }

	/// <summary>
	/// H股
	/// </summary>
	public float HGu { get; set; }

	/// <summary>
	/// 职工股
	/// </summary>
	public float ZhiGongGu { get; set; }

	/// <summary>
	/// 总资产
	/// </summary>
	public float ZongZiChan { get; set; }

	/// <summary>
	/// 流动资产
	/// </summary>
	public float LiuDongZiChan { get; set; }

	/// <summary>
	/// 固定资产
	/// </summary>
	public float GuDingZiChan { get; set; }

	/// <summary>
	/// 无形资产
	/// </summary>
	public float WuXingZiChan { get; set; }

	/// <summary>
	/// 股东人数
	/// </summary>
	public float GuDongRenShu { get; set; }

	/// <summary>
	/// 流动负债
	/// </summary>
	public float LiuDongFuZhai { get; set; }

	/// <summary>
	/// 长期负债
	/// </summary>
	public float ChangQiFuZhai { get; set; }

	/// <summary>
	/// 资本公积金
	/// </summary>
	public float ZiBenGongJiJin { get; set; }

	/// <summary>
	/// 净资产
	/// </summary>
	public float JingZiChan { get; set; }

	/// <summary>
	/// 主营业务收入
	/// </summary>
	public float ZhuYingShouRu { get; set; }

	/// <summary>
	/// 主营业务利润
	/// </summary>
	public float ZhuYingLiRun { get; set; }

	/// <summary>
	/// 应收账款
	/// </summary>
	public float Yingshouzhangkuan { get; set; }

	/// <summary>
	/// 营业利润
	/// </summary>
	public float YingyeLiRun { get; set; }

	/// <summary>
	/// 投资收益
	/// </summary>
	public float TouZiShouYu { get; set; }

	/// <summary>
	/// 经营现金流
	/// </summary>
	public float JingYingxianJinLiu { get; set; }

	/// <summary>
	/// 总现金流
	/// </summary>
	public float ZongXianJinLiu { get; set; }

	/// <summary>
	/// 存货
	/// </summary>
	public float CunHuo { get; set; }

	/// <summary>
	/// 利润总额
	/// </summary>
	public float LiRunZongHe { get; set; }

	/// <summary>
	/// 税后利润
	/// </summary>
	public float ShuiHouLiRun { get; set; }

	/// <summary>
	/// 净利润
	/// </summary>
	public float JingLiRun { get; set; }

	/// <summary>
	/// 未分配利润
	/// </summary>
	public float WeiFenLiRun { get; set; }

	/// <summary>
	/// 保留字段1
	/// </summary>
	public float BaoLiu1 { get; set; }

	/// <summary>
	/// 保留字段2
	/// </summary>
	public float BaoLiu2 { get; set; }

	/// <summary>
	/// 从字节数组反序列化
	/// </summary>
	/// <param name="data">原始数据</param>
	public void Unmarshal(byte[] data)
	{
		using (var ms = new MemoryStream(data))
		using (var reader = new BinaryReader(ms))
		{
			// 读取Unknown1（2字节）
			Unknown1 = reader.ReadBytes(2);

			// 读取市场类型（1字节）
			Market = reader.ReadByte();

			// 读取股票代码（6字节，ASCII编码）
			byte[] codeBytes = reader.ReadBytes(6);
			Code = Encoding.ASCII.GetString(codeBytes).TrimEnd('\0');

			// 读取财务数据（小端序，float32对应C#的float）
			LiuTongGuBen = reader.ReadSingle();
			Province = reader.ReadUInt16();
			Industry = reader.ReadUInt16();
			UpdatedDate = reader.ReadUInt32();
			IPODate = reader.ReadUInt32();
			ZongGuBen = reader.ReadSingle();
			GuoJiaGu = reader.ReadSingle();
			FaQiRenFaRenGu = reader.ReadSingle();
			FaRenGu = reader.ReadSingle();
			BGu = reader.ReadSingle();
			HGu = reader.ReadSingle();
			ZhiGongGu = reader.ReadSingle();
			ZongZiChan = reader.ReadSingle();
			LiuDongZiChan = reader.ReadSingle();
			GuDingZiChan = reader.ReadSingle();
			WuXingZiChan = reader.ReadSingle();
			GuDongRenShu = reader.ReadSingle();
			LiuDongFuZhai = reader.ReadSingle();
			ChangQiFuZhai = reader.ReadSingle();
			ZiBenGongJiJin = reader.ReadSingle();
			JingZiChan = reader.ReadSingle();
			ZhuYingShouRu = reader.ReadSingle();
			ZhuYingLiRun = reader.ReadSingle();
			Yingshouzhangkuan = reader.ReadSingle();
			YingyeLiRun = reader.ReadSingle();
			TouZiShouYu = reader.ReadSingle();
			JingYingxianJinLiu = reader.ReadSingle();
			ZongXianJinLiu = reader.ReadSingle();
			CunHuo = reader.ReadSingle();
			LiRunZongHe = reader.ReadSingle();
			ShuiHouLiRun = reader.ReadSingle();
			JingLiRun = reader.ReadSingle();
			WeiFenLiRun = reader.ReadSingle();
			BaoLiu1 = reader.ReadSingle();
			BaoLiu2 = reader.ReadSingle();
		}
	}
}

